from paddleocr import PaddleOCR,draw_ocr
from PIL import Image,ImageFont,ImageDraw
import matplotlib.pyplot as plt
import paddleocr

# Paddleocr目前支持的多语言语种可以通过修改lang参数进行切换
# 例如`ch`, `en`, `fr`, `german`, `korean`, `japan`
# ocr = PaddleOCR(use_angle_cls=True, lang="ch")  # need to run only once to download and load model into memory
# img_path = './test/11.jpeg'
# result = ocr.ocr(img_path, cls=True)
# # print(result)
# for line in result:
#     print(line)

# # 显示结果
# # from PIL import Image

# image = Image.open(img_path).convert('RGB')
# boxes = [line[0] for line in result]
# txts = [line[1][0] for line in result]
# scores = [line[1][1] for line in result]

# draw = ImageDraw.Draw(image) # 图片上打印

# font = ImageFont.truetype("simhei.ttf", 20, encoding="utf-8") # 参数1：字体文件路径，参数2：字体大小

# draw.text((0, 0), "Hi,我是诗shi", (255, 0, 0), font=font) 

# plt.figure('id card')
# plt.imshow(draw)
# plt.show()
# print(help(draw_ocr))
# im_show = draw_ocr(image, boxes, txts, scores, font_path='main/simfang.ttf')

# im_show = Image.fromarray(im_show)
# im_show.save('result.jpg')

def identifyIDCard(image_path:str,threshold:float = 0.7,lang:str = 'ch',use_angle_cls:bool=True):
    """[识别身份证信息]

    Args:
        image_path (str): [图片路径]
        threshold (float, optional): [文字置信度]. Defaults to 0.7.
        lang (str, optional): [识别的语言]. Defaults to 'ch'.
        use_angle_cls (bool, optional): [是否需要对角度反转识别]. Defaults to True.
    """    
    ocr = PaddleOCR(use_angle_cls=use_angle_cls,lang=lang)
    result = ocr.ocr(image_path, cls=True)
    for item in result:
        if item[1][1] < threshold:
            print('识别信息模糊，还请重新拍摄身份信息')
    txts = [line[1][0] for line in result]
    # info = {
    #     "name":None,
    #     "gender":None,
    #     "birthday":None,
    #     "path":None,
    #     "id":None,
    #     "nation":None,
    #     "validity":None,
    #     "agency":None
    #     }
    return txts


        
        
if __name__ == '__main__':
    txts = identifyIDCard('./test/aobama.jpeg',is_forward=True)
    print(txts)